(function($) {

	$.fn.isInfoSettingForm = function(){
		
		var changeAvatarButton = $('.upload_btn', $('#right_content')),
			itemWrap = $('.item-wrap', $('#right_content'));
		
		return $(this).each(function(){
			
			$(changeAvatarButton).wokaobox({
				'width'	: 535,
				'height': 444,
		        'type'	: 'iframe',
		        'afterClose'	: function(){
					location.reload();
				}
		    });
			
			jQuery.validator.addMethod("isQQ", function(value, element) { 
			    return this.optional(element) || /^\d{5,10}$/.test(value);
			}, "请输入正确格式的QQ号");
			
			jQuery.validator.addMethod("isWeibo", function(value, element) { 
                return this.optional(element) || /^(http:\/\/)?(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value);
			}, "请输入正确格式的微博地址");
			
            jQuery.validator.addMethod("nowhitespace", function(value, element) {
            	return this.optional(element) || /^\S+$/i.test(value);
            }, "No white space please"); 
                
			$('#form').validate({
		        errorElement: 'span',
		        onkeyup: false,
		        submitHandler: function(form){

		            $(form).ajaxSubmit({
		                url: $(this).attr('action'),
		                type: 'post',
		                dataType: 'json',
		                data: $(this).serialize(),
		                success: function(json){
		                    var position = $('.save_btn').offset();
		                    if (json.success) {
		                        $('#save-pop .save-success').show();
		                        
		                        //nickname
		                        $('#nickname').hide();
			                    $('#saveNickname').hide();
			                    $('#cancelUpdateNickname').hide();
			                    $('#showNickname').text($('#nickname').val()).show();
			                    $('#nicknameEdit').show();
			                    
		                        //frequentEmail
			                    $('#frequentEmail').hide();
				                $('#saveFrequentEmail').hide();
				                $('#cancelUpdate').hide();
				                $('#showFrequentEmail').text($('#frequentEmail').val()).show();
				                if ($('#frequentEmail').val() === '') {
				                    $('#frequentEmailEdit').text(messages.add).attr('style', 'margin-left: o;');
				                }
				                else {
				                    $('#frequentEmailEdit').text(messages.edit).attr('style', 'margin-left: 10px;');
				                }
				                $('#frequentEmailEdit').show();
		                    }
		                    else {
		                        $('#save-pop .save-success').hide();
		                        $('#save-pop .save-fail').show();
		                    }
		                    
		                    $('#save-pop').css('position', 'absolute')
		                    			  .css('top', position.top - 10)
		                    			  .css('left', position.left + 100)
		                    			  .css('background','#ffffff')
		                    			  .fadeIn().delay(500).fadeOut(500);
		                }
		            });
		        },
		        errorPlacement: function(error, element){

		        	if(element.hasClass('error')){
		        		
		        		var item = element.parents('.item');
			        	$('select', $(item)).hide();
			        	$('.tips', $(item)).hide();
			        	$('.error-wrap', $(item)).append(error).show();
		        	}
		        },
		        success: function(){
					$('.item').each(function(index){
		        		
						var item = $(this),
		        			inputElement = $('input[type="text"]', $(this));

		        		if(inputElement.attr('class') !== undefined){
		        			if(inputElement.hasClass('error')){
				        		
		        				$('.tips', $(item)).hide();
				        		$('select', $(item)).hide();
		        			}
		        			else{
		        				
		        				$('.error-wrap', $(item)).empty().hide();
				        		$('.tips', $(item)).hide();
				        		$('select', $(item)).show();
		        			}
		        		}
		        	});
		        },
		        rules: {
		            nickname: {
		            	required: true,
		                rangelength: [2, 20],
		                nowhitespace:true,
		                remote: {
		                    url: $('#context-path').val()+"/nickname/verify",
		                    type: "post",
		                    dataType: "json",
		                    data: $("#nickname").attr('name') + "=" + $("#nickname").val()
		                }
		            },
		            weibo: {
		            	isWeibo: true
		            },
		            qq: {
		            	isQQ: true
		            },
		            msn: {
		            	email: true
		            }
		        },
		        messages: {
		            nickname: {
		                required: messages.validRequired,
		                rangelength: messages.nicknameLength,
		                nowhitespace:'昵称只支持中英文,数字,下划线和减号'
		            },
		            frequentEmail: {
		                email: messages.invalidEmail
		            },
		            weibo: {
		            	required: messages.validRequired
		            },
		            msn: {
		            	email: '请输入正确格式的MSN帐号'
		            },
		            signature: {
		            	maxlength: "签名过长"
		            },
		            introduction: {
		            	maxlength: "简介过长"
		            }
		        }
		    });
			
			$('input[type="text"]', $(itemWrap)).focusin(function(){
				
				if(!$(this).hasClass('error')){
					
					$('.' + $(this).attr('name') + '-toggle', $(this).parents('.item')).toggle();
				}
        	});
			$('input[type="text"]', $(itemWrap)).focusout(function(){
        		
        		$('.tips', $(this).parents('.item')).hide();
        		if(!$(this).hasClass('error')){
        			$('select', $(this).parents('.item')).show();
        		}
        	});
			
			//修改邮箱
			$('#frequentEmailEdit').click(function(){
			    $('#showFrequentEmail').hide();
			    $('#frequentEmailEdit').hide();
			    $('#frequentEmail').removeAttr('disabled').show();
			    $('#cancelUpdate').show();
			    $('#saveFrequentEmail').show();
			    return false;
			});
			
			$('#cancelUpdate').click(function(){
			    $('#frequentEmail').attr('disabled', 'disabled').hide();
			    $('#cancelUpdate').hide();
			    $('#saveFrequentEmail').hide();
			    $('#showFrequentEmail').show();
			    $('#frequentEmailEdit').show();
			    $('.error-wrap', $(this).parents('.item')).empty().hide();
			    return false;
			});

			$('#saveFrequentEmail').click(function(){
			    var email = $('#frequentEmail');
			    $.ajax({
			    	async:false,
			        url: $('#saveFrequentEmail').attr('href'),
			        dataType: 'json',
			        type: 'POST',
			        data: "frequentEmail=" + email.val(),
			        success: function(json){
			            if (json.success) {
			                email.val(json.frequentEmail).hide();
			                $('#showFrequentEmail').show().text(json.frequentEmail);
			                $('#saveFrequentEmail').hide();
			                $('#cancelUpdate').hide();
			                if (json.frequentEmail == null || json.frequentEmail == '') {
			                    $('#frequentEmailEdit').text(messages.add).attr('style', 'margin-left: o;');
			                }
			                else {
			                    $('#frequentEmailEdit').text(messages.edit).attr('style', 'margin-left: 10px;');
			                }
			                $('#frequentEmailEdit').show();
			            }
			            else{
			            	email.parents('.item').find('select').hide();
			            	email.parents('.item').find('.tips').hide();
//			            	email.parents('.item').find('.error-wrap').append('<span for="frequentEmail" generated="true" class="error">'+ json.message+'</span>').show();
			            }
			        }
			    });
			    return false;
			});
			
			//修改昵称
		    $('#nicknameEdit').click(function(){
		        $('#showNickname').hide();
		        $('#nicknameEdit').hide();
		        $('#nickname').val($('#showNickname').text()).show();
		        $('#cancelUpdateNickname').show();
		        $('#saveNickname').show();
		        return false;
		    });
		    
		    $('#cancelUpdateNickname').click(function(){
		        $('#nickname').val($('#showNickname').text()).hide();
		        $('#cancelUpdateNickname').hide();
		        $('#saveNickname').hide();
		        $('#showNickname').show();
		        $('#nicknameEdit').show();
		        $('.error-wrap', $(this).parents('.item')).empty().hide();
		        return false;
		    });
		    
		    $('#saveNickname').click(function(){
		        var nickname = $('#nickname');
		        $.ajax({
		        	async:false,
		            url: $('#saveNickname').attr('href'),
		            dataType: 'json',
		            type: 'POST',
		            data: nickname.attr('name') + "=" + nickname.val(),
		            success: function(json){
		                if (json.success) {
		                    nickname.val(json.nickname).hide();
		                    $('#showNickname').show().text(json.nickname);
		                    $('#saveNickname').hide();
		                    $('#cancelUpdateNickname').hide();
		                    $('#nicknameEdit').show();
		                }
		                else{
		                	nickname.parents('.item').find('.error-wrap').empty().append('<span for="nickname" generated="true" class="error">'+ json.message+'</span>').show();
		                }
		            }
		        });
		        
		        return false;
		    });
		    
			$.get($('#context-path').val()+'/save/popup', function(html){ 
		        $('body').append(html);
		    });
		});
	};
})(jQuery);